﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Radar
{
    public partial class Form1 : Form
    {
        // khai bao anten quay
        System.Windows.Forms.Timer t = new Timer();
        int WIDTH = 460, HEIGHT = 460, vetquet = 230;
        int u; //do

        int cx, cy; // tam man hinh
        int x, y; //toa do
        int tx, ty, lim = 1; // ten do hoa ; lim so duong quet
        Bitmap btm;
        Pen p;
        Graphics g;

        int sovongtron = 3;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // radar quay
            btm = new Bitmap(WIDTH + 1, HEIGHT + 1);
            // mau xung quanh
            this.BackColor = Color.Black;
            //trung tam
            //pictureBox2.Visible;
            cx = WIDTH / 2;
            cy = HEIGHT / 2;
            //do bat dau
            u = 0;
            t.Interval = 5;
            t.Tick += new EventHandler(this.t_Tick);
            t.Start();

        }
        private void t_Tick(object sender, EventArgs e)
        {            
            // vong- pen 
            p = new Pen(Color.Green, 1f);
            g = Graphics.FromImage(btm);
            // tinh toan toa do
            int tu = (u - lim) % 360;
            if (u >= 0 && u <= 180)
            {
                // chuyen do sang radian
                x = cx + (int)(vetquet * Math.Sin(Math.PI * u / 180));
                y = cy - (int)(vetquet * Math.Cos(Math.PI * u / 180));
            }
            else
            {
                x = cx - (int)(vetquet * -Math.Sin(Math.PI * u / 180));
                y = cy - (int)(vetquet * Math.Cos(Math.PI * u / 180));
            }
            if (tu >= 0 && tu <= 180)
            {
                // chuyen do sang radian
                tx = cx + (int)(vetquet * Math.Sin(Math.PI * tu / 180));
                ty = cy - (int)(vetquet * Math.Cos(Math.PI * tu / 180));
            }
            else
            {
                tx = cx - (int)(vetquet * -Math.Sin(Math.PI * tu / 180));
                ty = cy - (int)(vetquet * Math.Cos(Math.PI * tu / 180));
            }

            if (sovongtron==3)
            {
                 //ve vong tron
                p.Clone();
                g.DrawEllipse(p, 0, 0, WIDTH, HEIGHT); // vong tron lon nhat
                g.DrawEllipse(p, 77, 77, WIDTH - 153, HEIGHT - 153);
                g.DrawEllipse(p, 106, 106, WIDTH - 212, HEIGHT - 212);
                g.DrawEllipse(p, 153, 153, WIDTH - 306, HEIGHT - 306); // vong tron nho nhat
            }
            if (sovongtron==5)
            {
                
                g.DrawEllipse(p, 0, 0, WIDTH, HEIGHT); // vong tron lon nhat
            g.DrawEllipse(p, 46, 46, WIDTH - 92, HEIGHT - 92);
            g.DrawEllipse(p, 92, 92, WIDTH - 184, HEIGHT - 184);
            g.DrawEllipse(p, 138, 138, WIDTH - 276, HEIGHT - 276);
            g.DrawEllipse(p, 184, 184, WIDTH - 368, HEIGHT - 368); // vong tron nho nhat
            }
            if (sovongtron == 6)
            {
                g.DrawEllipse(p, 0, 0, WIDTH, HEIGHT); // vong tron lon nhat
                g.DrawEllipse(p, 38, 38, WIDTH - 76, HEIGHT - 76);
                g.DrawEllipse(p, 76, 76, WIDTH - 153, HEIGHT - 153);
                g.DrawEllipse(p, 115, 115, WIDTH - 230, HEIGHT - 230);
                g.DrawEllipse(p, 153, 153, WIDTH - 306, HEIGHT - 306);
                g.DrawEllipse(p, 191, 191, WIDTH - 382, HEIGHT - 382); // vong tron nho nhat
            }

            // ve duong ke
            //p.Color = Color.Red;
            Pen pnew = new Pen(Color.Red);
            g.DrawLine(pnew, new Point(cx, 0), new Point(cx, HEIGHT));
            g.DrawLine(pnew, new Point(0, cy), new Point(WIDTH, cy));
            g.DrawLine(pnew, new Point(cx + 120, 35), new Point(cx - 120, 415));
            g.DrawLine(pnew, new Point(cx - 120, 35), new Point(cx + 120, 415));
            g.DrawLine(pnew, new Point(cy + 360, 35), new Point(cy - 360, 415));
            g.DrawLine(pnew, new Point(cy - 360, 35), new Point(cy + 360, 415));

            //g.DrawLine(p, new Point(cx+360, 35), new Point(cx -360, 415));
            //g.DrawLine(p, new Point(cx-360, 35), new Point(cx +360, 415));
            // ve duong quet
            g.DrawLine(new Pen(Color.Black, 1f), new Point(cx, cy), new Point(tx, ty));

            g.DrawLine(p, new Point(cx, cy), new Point(x, y));

            // load bitmap len pic1
            pictureBox1.Image = btm;
            // sap xep bo tri
            p.Dispose();
            g.Dispose();
            //udate
            u++;
            if (u == 360)
            {
                u = 0;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            sovongtron = 3;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sovongtron = 5;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            sovongtron = 6;
        }

       
    }
}
